package com.weishuang.monitor.leader.config.security.handler;

import cn.hutool.json.JSONUtil;
import com.weishuang.monitor.leader.commons.entity.Result;
import com.weishuang.monitor.leader.commons.enums.ICODE;
import com.weishuang.monitor.leader.commons.utils.ResultUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        Result info = ResultUtils.info(ICODE.LOGIN_ERROR);
        httpServletResponse.setStatus(401);
        httpServletResponse.setHeader("Content-type","application/json;charset=UTF-8");
        httpServletResponse.getWriter().write(JSONUtil.toJsonStr(info));
    }
}
